import * as dd from "dingtalk-jsapi";
import router from '@/router'
export default {
  methods: {
    androidBackBtn(self) {
      dd.ready(function () {
        //返回按钮点击的事件监听(android)
        /* document.addEventListener('backbutton', function (e) {
          e.preventDefault();
          const instance = self.$route
          console.log(self.$route);
          console.log(self);
          if (instance.name === 'Home') {
            const HomeInstance = self.$children[0]
            if (HomeInstance) {
              if (HomeInstance.isSearchGlobalShow) {
                HomeInstance.isSearchGlobalShow = false
                self.$bus.$emit('isShowNavigation', false)
              } else {
                dd.biz.navigation.close({})
              }
            }
          } else {
            window.history.go(-1)
          }
        }, false); */
        const clickFunction = (e) => {
          e.preventDefault();
          const instance = self.$route
          console.log(instance);

          if (instance.name === 'Home') {
            const HomeInstance = self.$children[0]
            if (HomeInstance) {
              if (HomeInstance.isSearchGlobalShow) {
                HomeInstance.isSearchGlobalShow = false
                self.$bus.$emit('isShowNavigation', false)
              } else {
                dd.biz.navigation.close({})
              }
            }
          }
          else if (instance.name === 'SP') {
            localStorage.setItem('spStatus', 2)
            window.location.reload()
          } else if (instance.name === 'DetailPage') {
            localStorage.setItem('backItemLayout',2)
            window.location.reload()
          } else {
            // window.history.go(-1)
            router.go(-1)
          }
          document.removeEventListener("backbutton", clickFunction, false);
        }
        document.addEventListener("backbutton", clickFunction, false);
      })
    },
    iosBackBtn(self) {
      dd.ready(function () {
        dd.biz.navigation.setLeft({
          control: true,
          text: '',
          onSuccess: (result) => { //IOS调用方法 ios返回不起作用，需要手动让他返回window.history.go(-1)
            const instance = self.$route
            if (instance.name === 'Home') {
              const HomeInstance = self.$children[0]
              if (HomeInstance) {
                if (HomeInstance.isSearchGlobalShow) {
                  HomeInstance.isSearchGlobalShow = false
                  self.$bus.$emit('isShowNavigation', false)
                } else {
                  dd.biz.navigation.close()
                }
              }
            } else {
              dd.biz.navigation.goBack()
            }
          }
        })
      })
    },
    gobackFilter(self) {
      if (dd.env.platform !== "notInDingTalk") {
        dd.ready(() => {
          /* -------------------------------- */
          const u = navigator.userAgent
          const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 // 判断是否是 android终端
          if (isAndroid) {
            this.androidBackBtn(self)
          } else {
            //'ios'
            this.iosBackBtn(self)
          }
          //返回按钮点击的事件监听(android)

          /* -------------------------------- */
        })
      }
    }
  },
  mounted() {
    this.gobackFilter(this)
  },
  activated() {
    this.gobackFilter(this)
  },
}